<?php
namespace Controllers;

require_once('\Models\Install.php');
require_once('\Views\InstallView.php');
require_once('\Views\PageView.php');
require_once('\Models\Password.php');
require_once('\Common\Validator.php');
require_once('\Common\Strings.php');
		
class InstallController
{
	public function createControl()
	{
		$pv = new \Views\PageView();
		$iv = new \Views\InstallView();
		$inst = new \Models\Install();
		$html ="";
		
		if(file_exists('config.php'))
		{
			$html .= $pv->encapsulateString("h1", \Common\Strings::HEADER_INSTALL);
			require_once('config.php');
			
			// Om konfigurationsfilen är korrekt ifylld
			if(
			defined("DB") && DB != "" &
			defined("DB_USER") && DB_USER != "" && 
			defined("DB_USER_PASSWORD") && DB_USER_PASSWORD != "" && 
			defined("DB_HOST") && DB_HOST != "" &&
			defined("DB_USER_PASSWORD") &&  
			defined("DB_CHARSET") && DB_CHARSET != "")
			{
				if(!$inst->installed(DB_HOST, DB, DB_USER, DB_USER_PASSWORD))
				{
					// Om formuläret har postats och försökt skapa databasen
					if($pv->valueIsSet($iv->tryCreateDB))
					{
						$errors = array();
						
						$userName = $pv->getValue($pv->userName);
						$firstName = $pv->getValue($pv->firstName);
						$lastName = $pv->getValue($pv->lastName);
						$email = $pv->getValue($pv->email);
						$password = $pv->getValue($pv->password);
						$repPassword = $pv->getValue($pv->repeatPassword);
						
						if(!\Common\Validator::validateUserName($userName))
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_USERNAME_INCORRECT, false);
						
						if(!\Common\Validator::validateName($firstName))
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_FIRSTNAME_INCORRECT, false);
						
						if(!\Common\Validator::validateName($lastName))
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_LASTNAME_INCORRECT, false);
						
						if(!\Common\Validator::validateEmail($email))
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_EMAIL_INCORRECT, false);
						
						if(!\Common\Validator::validatePassword($password, 8))
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_PASSWORD_INCORRECT, false);
						
						if(!\Common\Validator::validatePassword($repPassword, 8))
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_PASSWORD_INCORRECT, false);
						
						if($password != $repPassword)
							$errors[] = $pv->setStatusText(\Common\Strings::VALIDATION_PASSWORD_MISMATCH, false);
						
						if(count($errors)==0)
						{
							$hashedPassword = \Models\Password::hashPassword($password);
							
							// Kör installations-queries
							if($inst->install(
							DB_HOST, 
							DB, 
							DB_USER, 
							DB_USER_PASSWORD, 
							DB_CHARSET, 
							$userName, 
							$firstName, 
							$lastName, 
							$hashedPassword, 
							$email) == true)
							{
								// Ta bort config-filen
								unlink('config.php');
								
								// Spara anslutningsuppgifter i php-class
								$filename = '.\Models\DBConfig.php';
								$content = "<?php \nnamespace Models; \n\nclass DBConfig \n{ \n\tpublic \$m_host = '".DB_HOST."'; \n\tpublic \$m_user = '".DB_USER."'; \n\tpublic \$m_password = '".DB_USER_PASSWORD."'; \n\tpublic \$m_db = '".DB."'; \n} \n?>";
								 
								$handle = fopen($filename, "w");
								fwrite($handle, $content);
								fclose($handle);
								
								$html .= $pv->setStatusText(\Common\Strings::DB_CREATE_SUCCES, true);
								$html .= $iv->getContinueButton();
							}
							else
								$html .= $pv->setStatusText(\Common\Strings::DB_CREATE_FAIL, false);
						}
						else
						{
							$html .= $pv->printErrors($errors);
							$html .= $iv->getInstallForms();
						}
					}
					// Formuläret har ännu ej postats
					else
					{
						$html .= $pv->encapsulateString("p", \Common\Strings::TEXT_INSTALL_INFO);
						$html .= $iv->getInstallForms();
					}
				}
				// Starta applikation om den redan är installerad
				else
				{
					require_once('start.php');
					exit;
				}
			}
			else
				$html .= $pv->setStatusText(\Common\Strings::CONFIG_FILE_ERROR, false);
		}
		
		// Om config.php inte finns så borde applikationen var installerad
		else
		{
			// Inkludera för att inte systemet ska krascha om config.php saknas och inte DBConfig är skapad.
			include_once('\Models\DBConfig.php');
			$dbc = new \Models\DBConfig();
			
			if(!$inst->installed($dbc->m_host, $dbc->m_db, $dbc->m_user, $dbc->m_password))
			{
				$html .= $pv->encapsulateString("h1", HEADER_INSTALL);
				$html .= $pv->setStatusText(\Common\Strings::CONFIG_FILE_MISSING, false);
			}
			else
			{
				require_once('start.php');
				exit;
			}
				
		}
		
		return $pv->generateHTMLPage(\Common\Strings::TITLE_INSTALLATION, $html);
	}
}

?>